﻿using System;
using System.Collections.Generic;
using System.Text;
using OrdersLU;

namespace DIProject.Util.Orders
{
    public class PricesLoad
    {
        public void Load()
        {
            Util.SettingFile sf = Util.Setting2.GetSetting();
            List<Util.OrderInfo> orders = sf.Orders;
            foreach (Util.OrderInfo o in orders)
            {
                if (o.Note != "2")
                    continue;
                switch (o.PriceType)
                {
                    case PriceType.MORON:
                        //DeletePrices(o.ContragentId);
                        DoMoron(o);
                        break;
                    case PriceType.PROTEK:
                       // DeletePrices(o.ContragentId);
                        DoProtek(o);
                        break;
                    case PriceType.APTEKA2002:
                        DoApteka2002(o);
                        break;
                    case PriceType.KATREN:
                        DoKatren(o);
                        break;
                    case PriceType.SIA:
                        DoSia(o);
                        break;
                    case PriceType.A_HOLDING:
                        DoAHolding(o);
                        break;
                    default:
                        break;
                }
            }
        }
        private void PriceUtil(Util.OrderInfo o, OrdersLU.InDB db)
        {
            db.ArhivePrice(o.ContragentId);
            db.PriceInfoInsert(o.ContragentId, DateTime.Now, string.Empty, FirstForm.Login);
            
        }
        private void DoSia(OrderInfo o)
        {
            try
            {
                OrdersLU.Prices.Sia sia = new OrdersLU.Prices.Sia();
                sia.ContragentId = o.ContragentId;
                sia.Path = o.PricePath;
                List<PriceItem> list = sia.LoadData();
                InDB db = new InDB();
                db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
                this.PriceUtil(o, db);
                db.PriceItemsInsert(list);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }
        private void DoAHolding(OrderInfo o)
        {
            try 
            {
                OrdersLU.Prices.AHolding a = new OrdersLU.Prices.AHolding();
                a.ContragentId = o.ContragentId;
                a.Path = o.PricePath;
                List<OrdersLU.PriceItem> pis = a.LoadData();
                OrdersLU.InDB db = new InDB();
                db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
                PriceUtil(o, db);
                db.PriceItemsInsert(pis);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }

        private void DoKatren(Util.OrderInfo o)
        {
            try
            {
                OrdersLU.Prices.Katren k = new OrdersLU.Prices.Katren();
                k.ContragentId = o.ContragentId;
                k.Path = o.PricePath;
                List<OrdersLU.PriceItem> pis = k.LoadData();
                OrdersLU.InDB db = new InDB();
                db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
                PriceUtil(o, db);
                db.PriceItemsInsert(pis);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }
        private void DoMoron(Util.OrderInfo o)
        {
           // return;
            try
            {
                
                OrdersLU.Prices.Moron m = new OrdersLU.Prices.Moron();
                m.ContragentId = o.ContragentId;
                m.Path = o.PricePath;
                List<OrdersLU.PriceItem> pis = m.LoadData();
                OrdersLU.InDB db = new InDB();
                db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
                PriceUtil(o, db);
                db.PriceItemsInsert(pis);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }
        private void DoApteka2002(Util.OrderInfo o)
        {
          //  return;
            OrdersLU.Prices.Apteka2002 a = new OrdersLU.Prices.Apteka2002();
            a.ContragentId = o.ContragentId;
            a.Path = o.PricePath;
            List<OrdersLU.PriceItem> pis = a.LoadData();
            OrdersLU.InDB db = new InDB();
            db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
            PriceUtil(o, db);
            db.PriceItemsInsert(pis);
        }
        private void DoProtek(Util.OrderInfo o)
        {
           // return;
            try
            {
                OrdersLU.Prices.Protek m = new OrdersLU.Prices.Protek();
                m.ContragentId = o.ContragentId;
                m.Path = o.PricePath;
                List<OrdersLU.PriceItem> pis = m.LoadData();
                OrdersLU.InDB db = new InDB();
                db.ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
                PriceUtil(o, db);
                db.PriceItemsInsert(pis);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }
        private void  DeletePrices(Int32 contragentId)
        {
            string QueryString = @"delete PR.PriceLinesNOTParsed where ContragentId = @ContragentId
delete PR.PriceLinesParsed where ContragentId = @ContragentId";
            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(Properties.Settings.Default.DIAptekaConnectionString))
            {
                try
                {
                    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(QueryString, connection);
                    command.Parameters.AddWithValue("@ContragentId", contragentId);
                    connection.Open();
                    Int32 e = command.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException se)
                {
                    string s = se.ToString();
                    throw se;
                }
                catch (Exception ee)
                {
                    string s = ee.ToString();
                    throw ee;
                }
            }
        }
        
    }
   
}
